package com.ruoyi.web.service.impl;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.config.pay.CommonFieldName;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.web.request.IESI123108Request;
import com.ruoyi.web.request.IESI123109Request;
import com.ruoyi.web.service.AgentPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.PayReceiptDownloadMapper;
import com.ruoyi.web.domain.PayReceiptDownload;
import com.ruoyi.web.service.IPayReceiptDownloadService;
import com.ruoyi.common.core.text.Convert;

/**
 * 商户代付电子回单Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-04
 */
@Service
public class PayReceiptDownloadServiceImpl implements IPayReceiptDownloadService 
{
    @Autowired
    private PayReceiptDownloadMapper payReceiptDownloadMapper;
    @Autowired
    private AgentPayService agentPayService;
    /**
     * 查询商户代付电子回单
     * 
     * @param sequenceNo 商户代付电子回单ID
     * @return 商户代付电子回单
     */
    @Override
    public PayReceiptDownload selectPayReceiptDownloadById(String sequenceNo)
    {
        return payReceiptDownloadMapper.selectPayReceiptDownloadById(sequenceNo);
    }

    /**
     * 查询商户代付电子回单列表
     * 
     * @param payReceiptDownload 商户代付电子回单
     * @return 商户代付电子回单
     */
    @Override
    public List<PayReceiptDownload> selectPayReceiptDownloadList(PayReceiptDownload payReceiptDownload)
    {
        return payReceiptDownloadMapper.selectPayReceiptDownloadList(payReceiptDownload);
    }

    /**
     * 新增商户代付电子回单
     * 
     * @param payReceiptDownload 商户代付电子回单
     * @return 结果
     */
    @Override
    public int insertPayReceiptDownload(PayReceiptDownload payReceiptDownload) throws Exception
    {
        BusinessException.notNull(payReceiptDownload.getCheckDate(),"请填写对账日期");
        IESI123109Request request =new IESI123109Request();
        request.setApplyNo(payReceiptDownload.getApplyNo());
        request.setCheckDate(payReceiptDownload.getCheckDate());
        request.setFileType(payReceiptDownload.getFileType());
        payReceiptDownload.setSequenceNo(payReceiptDownload.getApplyNo());
        HashMap<String, Object> resultMap= agentPayService.IESI123109(request,payReceiptDownload.getApplyNo(),payReceiptDownload.getMerchCd());
        String respCode = (String) resultMap.get(CommonFieldName.RESP_CODE);
        String respMsg = (String) resultMap.get(CommonFieldName.RESP_MSG);
        if (!"00000".equals(respCode)){
            BusinessException.notNull(null,respMsg);
        }
        payReceiptDownload.setCheckUrl((String) resultMap.get("downloadUrl"));
        PayReceiptDownload old =selectPayReceiptDownloadById(payReceiptDownload.getApplyNo());
        if (old!=null){
            return payReceiptDownloadMapper.updatePayReceiptDownload(payReceiptDownload);
        }
        return payReceiptDownloadMapper.insertPayReceiptDownload(payReceiptDownload);
    }

    /**
     * 修改商户代付电子回单
     * 
     * @param payReceiptDownload 商户代付电子回单
     * @return 结果
     */
    @Override
    public int updatePayReceiptDownload(PayReceiptDownload payReceiptDownload)
    {
        return payReceiptDownloadMapper.updatePayReceiptDownload(payReceiptDownload);
    }

    /**
     * 删除商户代付电子回单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePayReceiptDownloadByIds(String ids)
    {
        return payReceiptDownloadMapper.deletePayReceiptDownloadByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商户代付电子回单信息
     * 
     * @param sequenceNo 商户代付电子回单ID
     * @return 结果
     */
    @Override
    public int deletePayReceiptDownloadById(String sequenceNo)
    {
        return payReceiptDownloadMapper.deletePayReceiptDownloadById(sequenceNo);
    }
}
